import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;


class Student {
    public int studentId;
    public int classId;
}

@WebServlet("/postParameter")
public class PostParameterService extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String studentId = req.getParameter("studentId");
//        String classId = req.getParameter("classId");
//        resp.setContentType("text/html");
//        resp.getWriter().write("studentId "+studentId+" classId "+classId);



//      通过这个方法来获取body内容的长度
//        int length = req.getContentLength();
//        提前构建好一个byte数组来存放body里的内容
//        byte[] buffer = new byte[length];
//      利用getInputStream方法获取到json数据的内容
//        InputStream inputStream = req.getInputStream();
//        inputStream.read(buffer);
//        String body = new String(buffer,0,length,"utf8");
//        System.out.println("body=" + body);
//        resp.getWriter().write(body);

//         ObjectMapper是jackson中的核心对象
        ObjectMapper objectMapper = new ObjectMapper();
//        readValue方法是将json格式的数据转化成java对象
        Student student = objectMapper.readValue(req.getInputStream(),Student.class);
        System.out.println(student.classId+","+student.studentId);
    }
}
